package com.ccxc.question.exam.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ccxc.question.exam.dao.QuestionDAO;
import com.ccxc.question.exam.dao.QuestionOptionDAO;
import com.ccxc.question.exam.dto.AreaDTO;
import com.ccxc.question.exam.dto.AreaDTO2;
import com.ccxc.question.exam.dto.QuestionDTO;

@Service
@Transactional
public class QuestionService {
    @Autowired
    private QuestionDAO questionDao;
    @Autowired
    private QuestionOptionDAO questionOptionDao;

    public QuestionDTO add(QuestionDTO questionDto) {
        questionDao.add(questionDto);
        questionOptionDao.add(questionDto.getId(), questionDto.getOptions());
        return questionDto;
    }

    @Transactional(readOnly = true)
    public QuestionDTO findById(String id) {
        QuestionDTO questionDto = questionDao.findById(id);
        if (null != questionDto) {
            questionDto.setOptions(questionOptionDao.findByTestId(id));
        }
        return questionDto;
    }

    public QuestionDTO update(QuestionDTO questionDto) {
        questionDao.update(questionDto);
        questionOptionDao.update(questionDto.getId(), questionDto.getOptions());
        return questionDto;
    }

    public void delete(String id) {
        int c = questionDao.delete(id);
        if (c > 0) {
            questionOptionDao.delete(id);
        }
    }

    /**
     * @DEscription 功能说明  随机抽取题目
     * 参数说明：
     */
    public QuestionDTO look() {
        QuestionDTO questionDto = questionDao.look();
        String id = questionDto.getId();
        if (null != questionDto) {
            questionDto.setOptions(questionOptionDao.findByTestId(id));
        }
        return questionDto;
    }

    /**
     * @DEscription 功能说明 根据选择的知识领域随机抽题
     * 参数说明：
     */
    public QuestionDTO findByarea(AreaDTO areaDTO) {
        QuestionDTO questionDto = questionDao.lookarea(areaDTO.getArea1(), areaDTO.getArea2(), areaDTO.getArea3());
        String id = questionDto.getId();
        if (null != questionDto) {
            questionDto.setOptions(questionOptionDao.findByTestId(id));
        }
        return questionDto;
    }

    /**
     * @DEscription 功能说明 查看全部的知识领域
     * 参数说明：
     */
    public List<AreaDTO2> area() {
        List<AreaDTO2> AreaDTO2 = questionDao.area();
        // TODO Auto-generated method stub
        return AreaDTO2;
    }
}
